package com.estar.integaration.Controller.paymentPlanController;


import com.alibaba.fastjson.JSONObject;
import com.estar.integaration.Service.paymentPlanService.IDontParseSupplierService;
import com.estar.integaration.Util.Util;
import com.estar.integaration.entity.NoSQL.Res;
import com.estar.integaration.entity.paymentPlanEntity.DontParseSupplier;
import com.estar.integaration.entity.paymentPlanEntity.SupplierAccountPeriod;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YJL
 * @since 2022-03-25
 */
@RestController
@RequestMapping("/dont-parse-supplier")
public class DontParseSupplierController {

  @Autowired
  IDontParseSupplierService dontParseSupplierService;

  /**
   * 获取全部
   * @param
   * @return
   */
  @GetMapping("/getall_dont")
  public Res getAllDontParseSupplier(){
    List<DontParseSupplier> dontParseSupplierList = dontParseSupplierService.getAllDontParseSupplier();
    return new Res("20000",new Gson().toJson(dontParseSupplierList));
  }

  /**
   * 删除
   */
  @PostMapping("/delete_dont")
  public Res deleteDPS(@RequestBody JSONObject jsonObject){
    String supplierName = jsonObject.getString("sName");
    dontParseSupplierService.deleteDontParseSupplier(supplierName);
    List<DontParseSupplier> dontParseSupplierList = dontParseSupplierService.getAllDontParseSupplier();
    return new Res("20000",new Gson().toJson(dontParseSupplierList));
  }

  /**
   * 查询
   */
  @PostMapping("/search_dont")
  public Res searchDPS(@RequestBody JSONObject jsonObject){
    String supplierName = jsonObject.getString("sName");
    DontParseSupplier dontParseSupplier = dontParseSupplierService.searchDontParseSupplier(supplierName);
    return new Res("20000",dontParseSupplier.getSupplierName());
  }

  @PostMapping("/insert_dont")
  public Res insertDPS(@RequestBody JSONObject jsonObject){
    String supplierName = jsonObject.getString("sName");
    DontParseSupplier dontParseSupplier = new DontParseSupplier(Util.getUUID(),supplierName);
   dontParseSupplierService.save(dontParseSupplier);
    List<DontParseSupplier> dontParseSupplierList = dontParseSupplierService.getAllDontParseSupplier();
    return new Res("20000",new Gson().toJson(dontParseSupplierList));
  }


  @PostMapping("/filter_supplier")
  public Res filterSupplier(@RequestBody JSONObject jsonObject){
    String sName = jsonObject.getString("sName");
    List<DontParseSupplier> dontParseSupplierList = null;
    if(sName== null || sName.equals("")){
      dontParseSupplierList = dontParseSupplierService.getAllDontParseSupplier();
    }else {
      dontParseSupplierList = dontParseSupplierService.filterDontSupplier(sName);
    }
    return new Res("20000",new Gson().toJson(dontParseSupplierList));
  }


}
